<template>
  <el-dialog :title="title" :visible.sync="dialogAddNew" width="50%" center>
    <el-form ref="storehouseForm" :model="storehouseForm" :rules="rules">
      <el-form-item label="仓库名称" :label-width="formLabelWidth" prop="warehouse_id">
        <el-select v-model="storehouseForm.warehouse_id" placeholder="请选择仓库名称" :disabled="isDisable">
          <el-option
            v-for="item in warehouseList"
            :key="item.id"
            :label="item.warehouse_name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库位名称" :label-width="formLabelWidth" prop="storehouse_name">
        <el-input v-model="storehouseForm.storehouse_name" autocomplete="off" :disabled="isDisable" />
      </el-form-item>
      <el-form-item label="名称描述" :label-width="formLabelWidth">
        <el-input v-model="storehouseForm.desc" autocomplete="off" :disabled="isDisable" />
      </el-form-item>
      <el-form-item label="库存能力" :label-width="formLabelWidth">
        <el-input v-model="storehouseForm.inventory" autocomplete="off" :disabled="isDisable" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogAddNew = false">取 消</el-button>
      <el-button type="primary" :disabled="isDisable" @click="addFun('storehouseForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { operationStorehouse } from '@/api/wms'
export default {
  props: {
    title: {
      type: String,
      default: '新增'
    },
    warehouseList: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      dialogAddNew: false,
      formLabelWidth: '80px',
      storehouseForm: {
        warehouse_id: '',
        storehouse_name: '',
        desc: '',
        inventory: ''
      },
      rules: {
        warehouse_id: [
          { required: true, message: '请选择活动区域', trigger: 'change' }
        ],
        storehouse_name: [
          { required: true, message: '请填写库位编号', trigger: 'blur' }
        ]
      },
      type: 1,
      isDisable: false
    }
  },
  created() {

  },
  methods: {
    addFun(formName) {
      // console.log('添加')
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = {
            type: this.type,
            pro: this.storehouseForm
          }
          console.log(data)
          operationStorehouse(data).then(res => {
            if (res.code === 200) {
              this.$message.success(this.type === 1 ? '库位信息上传成功' : '库位信息更新成功')
              this.dialogAddNew = false
              this.storehouseForm = {
                warehouse_id: '',
                storehouse_name: '',
                desc: '',
                inventory: ''
              }
              this.$emit('reloadData')
            } else {
              this.$message.info('库位信息上传失败，请重新上传')
            }
          })
        } else {
          console.log('error submit!!')
          // console.log(this.storehouseForm.warehouseId)
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
